﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using WebSiteAdminTool.UI.ViewModels.Base;
using WebSiteAdminTool.UI.Utils.Commands;
using WebSiteAdminTool.UI.Services;
using WebSiteAdminTool.UI.Views.Security;
using System.Linq;
using GalaSoft.MvvmLight.Messaging;
using WebSiteAdminTool.UI.MembershipService;
using WebSiteAdminTool.UI.Services.Navigation;
using WebSiteAdminTool.UI.Ioc;
using WebSiteAdminTool.UI.Services.Messaging;
using WebSiteAdminTool.UI.Utils;

namespace WebSiteAdminTool.UI.ViewModels.Security.Users
{
    public class UserManagementActionsViewModel : BaseNavigableViewModel
    {
        #region private data

        private UserData _currentUser = null;
        private UserPasswordConstraintsData _passwordConstraints = null;
        // commands
        private GenericCommand _createUserCommand;
        private GenericCommand _editUserCommand;
        private GenericCommand _removeUserCommand;
        private GenericCommand _resetPasswordCommand;
        private GenericCommand _updateQuestionAnswerCommand;

        #endregion

        #region ctors

        public UserManagementActionsViewModel()
        {
            base.InitViewModel();
        }

        #endregion

        #region public props

        private UserData CurentUser
        {
            get { return _currentUser; }
            set
            {
                _currentUser = value;
                EditUserCommand.IsEnabled =
                    RemoveUserCommand.IsEnabled =
                    ResetPasswordCommand.IsEnabled = null != _currentUser;
            }
        }
        public Visibility ResetPasswordVisibility
        {
            get
            {
                if (null != PasswordConstraints)
                    return PasswordConstraints.EnablePasswordReset ? Visibility.Visible : Visibility.Collapsed;
                return Visibility.Collapsed;
            }
        }
        public Visibility UpdateQuestionAnswerVisibility
        {
            get
            {
                if (null != PasswordConstraints)
                    return PasswordConstraints.RequiresQuestionAndAnswer ? Visibility.Visible : Visibility.Collapsed;
                return Visibility.Collapsed;
            }
        }

        private UserPasswordConstraintsData PasswordConstraints
        {
            get { return _passwordConstraints; }
            set
            {
                _passwordConstraints = value;
                if (null != value)
                {
                    RaisePropertyChanged("ResetPasswordVisibility");
                    RaisePropertyChanged("UpdateQuestionAnswerVisibility");
                }
            }
        }
        // commands
        public GenericCommand CreateUserCommand { get { return _createUserCommand; } }
        public GenericCommand EditUserCommand { get { return _editUserCommand; } }
        public GenericCommand RemoveUserCommand { get { return _removeUserCommand; } }
        public GenericCommand ResetPasswordCommand { get { return _resetPasswordCommand; } }
        public GenericCommand UpdateQuestionAnswerCommand { get { return _updateQuestionAnswerCommand; } }

        #endregion

        #region overriden base methods

        protected override void InitializeCommands()
        {
            _createUserCommand = new ActionLinkClickedCommand(this.CreateUser, true);
            _editUserCommand = new ActionLinkClickedCommand(this.EditUser);
            _removeUserCommand = new ActionLinkClickedCommand(this.RemoveUser);
            _resetPasswordCommand = new ActionLinkClickedCommand(this.ResetPassword);
            _updateQuestionAnswerCommand = new ActionLinkClickedCommand(this.UpdateQuestionAnswer);

            base.InitializeCommands();
        }

        protected override void RegisterListeners()
        {
            MessageManager.RegisterListener<SelectedItemChangedMessage<UserData>>(
                this, message => CurentUser = message.Content);

            base.RegisterListeners();
        }

        #endregion

        #region data loading methods

        protected override void LoadData()
        {
            base.LoadData();

            var client = new MembershipServiceClient();
            client.GetPasswordConstraintsCompleted += new EventHandler<GetPasswordConstraintsCompletedEventArgs>(client_GetPasswordConstraintsCompleted);
            client.GetPasswordConstraintsAsync();
        }

        void client_GetPasswordConstraintsCompleted(object sender, GetPasswordConstraintsCompletedEventArgs e)
        {
            if (e.IsServiceCallSuccessfull() && null != e.Result)
            {
                PasswordConstraints = e.Result;
            }
        }

        #endregion

        #region command handlers

        private void CreateUser(object data)
        {
            SecurityUiHelpers.CreateUserDialog();
        }

        private void EditUser(object data)
        {
            if (null != this.CurentUser)
            {
                SecurityUiHelpers.EditUserDialog(this.CurentUser);
            }
        }

        private void RemoveUser(object data)
        {
            SecurityUiHelpers.RemoveUser(this.CurentUser);
        }

        private void ResetPassword(object data)
        {
            if (null != this.CurentUser)
            {
                var client = new MembershipServiceClient();
                client.ResetPasswordCompleted += new EventHandler<ResetPasswordCompletedEventArgs>(client_ResetPasswordCompleted);
                client.ResetPasswordAsync(this.CurentUser.Id);
            }
        }

        void client_ResetPasswordCompleted(object sender, ResetPasswordCompletedEventArgs e)
        {
            if (e.IsServiceCallSuccessfull() && !string.IsNullOrWhiteSpace(e.Result))
            {
                Factory.ServiceLocator.MessageBoxService.Show(string.Format("The generated password is: {0}\nPlease provide it to user after the changes are saved.", e.Result), "New Password");
            }
        }

        private void UpdateQuestionAnswer(object data)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
